package com.goodboys.controller;

import com.goodboys.base.controller.BaseController;
import com.goodboys.base.entity.BaseReqBean;
import com.goodboys.base.resp.Response;
import com.goodboys.service.CommentService;
import com.goodboys.utils.JwtUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by CJ on 2018-05-27.
 */
@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController<CommentController>{

    private final Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentService commentService;

    @PostMapping("/addComment")
    @ResponseBody
    public Response addComment(BaseReqBean baseReqBean){
        Response response = new Response();
        try {
            Map result = commentService.addComment(baseReqBean);
            return getRetunResult(result,response);
        } catch (Exception e) {
            logger.error("新增评论失败。。。");
            e.printStackTrace();
            return response.failure("新增评论失败，请重试。。。");
        }
    }

    @PostMapping("/deleteComment")
    @ResponseBody
    public Response deleteComment(BaseReqBean baseReqBean, HttpServletRequest request){
        Response response = new Response();
        if(StringUtils.isEmpty(baseReqBean.getUserId())) return response.failure("用户id为空，请重试。。。");
        if(!JwtUtils.isPermission(baseReqBean.getUserId(),request)) return  response.failure("他人评论不可删除。。。");
        try {
            Map result = commentService.deleteComment(baseReqBean);
            return getRetunResult(result,response);
        } catch (Exception e) {
            logger.error("创建用户自定义标签失败。。。");
            e.printStackTrace();
            return response.failure("创建用户自定义标签失败，请重试。。。");
        }
    }


    @PostMapping("/likeComment")
    @ResponseBody
    public Response likeComment(BaseReqBean baseReqBean){
        Response response = new Response();
        try {
            Map result = commentService.likeComment(baseReqBean);
            return getRetunResult(result,response);
        } catch (Exception e) {
            logger.error("创建用户自定义标签失败。。。");
            e.printStackTrace();
            return response.failure("创建用户自定义标签失败，请重试。。。");
        }
    }


}
